package com.wave.service;

import com.alibaba.fastjson.JSONObject;
import com.wave.entity.User;
import com.wave.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public int create (User user) {
        return userMapper.create(user);
    }

    public int update(User user) {
        return userMapper.update(user);
    }

    public Integer selectAccount(String account){
        return userMapper.selectAccount(account);
    }
    public String selectAuth(String account){
        return userMapper.selectAuth(account);
    }
    //queryDetail为认证用户查询详细    userDetail为游客用户查询详细
    public User queryDetail(String account){
        return userMapper.queryDetail(account);
    }
    public User userDetail(String account){
        return userMapper.userDetail(account);
    }

    public String getPhone(String aId) {
        return userMapper.getPhone(aId);
    }
    public String getUserName(String account) {
        return userMapper.getUserName(account);
    }
    public JSONObject getUserList(int pageNum, int pageSize, String userName, String aid) {
        int pageStart = (pageNum - 1) * pageSize; //加载起始
        JSONObject userList = new JSONObject();
        List<User> list = userMapper.getUserList(pageStart, pageSize, userName, aid);
        int total = userMapper.getUserNum(userName, aid);
        userList.put("userList", list);
        userList.put("total", total);
        return userList;
    }
    public  List<User> list() {
        return userMapper.list();
    }

    public int bindAuth(String account, String aId, String phone) {
        return userMapper.bindAuth(account, aId, phone);
    }

    public JSONObject adminLogin(String select, String account, String password) {
        JSONObject respond = new JSONObject();
        String phone = "";
        if (select.equals("1")) {
            if (userMapper.selectAccount(account) == 0){
                respond.put("tips", "账号不存在！");
                respond.put("result", false);
                return respond;
            }else {
                if (!userMapper.selectType(account, phone).equals("1")){
                    respond.put("tips", "非管理员账号！");
                    respond.put("result", false);
                    return respond;
                }
                if (!password.equals(userMapper.selectPassword(account, phone))){
                    respond.put("tips", "密码错误！");
                    respond.put("result", false);
                    return respond;
                }
            }
            respond.put("user", userMapper.selectAdmin(account, phone));
        }else if (select.equals("2")) {
            if (userMapper.selectPhone(account) == 0){
                respond.put("tips", "手机号不存在！");
                respond.put("result", false);
                return respond;
            }else {
                phone = account;
                account = "";
                if (!userMapper.selectType(account, phone).equals("1")){
                    respond.put("tips", "非管理员手机号！");
                    respond.put("result", false);
                    return respond;
                }
                if (!password.equals(userMapper.selectPassword(account, phone))){
                    respond.put("tips", "密码错误！");
                    respond.put("result", false);
                    return respond;
                }
            }
            respond.put("user", userMapper.selectAdmin(account, phone));
        }
        respond.put("result", true);
        return respond;
    }

    public boolean alterPassword(String account, String newPassword, String oldPassword) {
        User user = userMapper.selectAdmin(account,  "");
        if (!user.getPassword().equals(oldPassword)) {
            return false;
        }
        user.setPassword(newPassword);
        userMapper.update(user);
        return true;
    }

    public int typeNum(String typeStr) {
        int type = Integer.valueOf(typeStr);
        return userMapper.getTypeNum(type);
    }
}
